﻿#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    int totalFruit(vector<int>& f) {
        int hash[100001] = { 0 }; // 统计窗⼝内出现了多少种⽔果
        int ret = 0;
        for (int left = 0, right = 0, kinds = 0; right < f.size(); right++) {
            if (hash[f[right]] == 0)
                kinds++;      // 维护⽔果的种类
            hash[f[right]]++; // 进窗⼝
            while (kinds > 2) // 判断
            {
                // 出窗⼝
                hash[f[left]]--;
                if (hash[f[left]] == 0)
                    kinds--;
                left++;
            }
            ret = max(ret, right - left + 1);
        }
        return ret;
    }
};

////////////////////////////////////////////////////////
class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> ret;
        int hash1[26] = { 0 };
        int m = p.size();
        for (auto ch : p)
            hash1[ch - 'a']++;
        int hash2[26] = { 0 };
        for (int left = 0, right = 0, count = 0; right < s.size(); right++) {
            char in = s[right];
            if (++hash2[in - 'a'] <= hash1[in - 'a'])
                count++;
            if (right - left + 1 > m) // 判断
            {
                char out = s[left++];
                // 出窗⼝ + 维护 count
                if (hash2[out - 'a']-- <= hash1[out - 'a'])
                    count--;
            }
            // 更新结果
            if (count == m)
                ret.push_back(left);
        }
        return ret;
    }
}
;